﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" Inherits="Pages_Operators_LocationList" Codebehind="LocationList.aspx.cs" %>

<asp:Content ID="Content1" ContentPlaceHolderID="title" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="using" runat="Server">

    <script type="text/javascript" src="/js/smartpaginator.js"></script>

    <link href="/css/smartpaginator.css" rel="stylesheet" type="text/css" />
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="header" runat="Server">
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="content" runat="Server">
    <div style="margin: 10px 3px 0 3px">
        <!-- Title -->
        <h1 style="text-indent: 10px">
            Location list</h1>
        <div id="location">
        </div>
        <div id="red" style="margin: auto;">
        </div>
    </div>

    <script type="text/javascript">
        // Variables

        $(document).ready(function () {

            // Data Methods
            function getData(pageNumber, pagelink) {
                loading($('#location'));

                $.ajax({
                    type: "POST",
                    url: "/Get.asmx/GetAllLocation",
                    contentType: "application/json; charset=utf-8",
                    data: '{ pageNumber: ' + pageNumber + ' }',
                    dataType: "json",
                    success: (function Success(data, status) {
                        var list = data.d;
                        $('#location').setTemplateURL("/Pages/Operators/template/locationListTemplate.htm");
                        $('#location').processTemplate(list);

                        if (pagelink != null) {
                            $('#red').smartpaginator({
                                totalrecords: list.TotalRecords,
                                recordsperpage: list.PageSize,
                                length: 8,
                                next: 'Next',
                                prev: 'Prev',
                                first: 'First',
                                last: 'Last',
                                theme: 'red',
                                controlsalways: true,
                                onchange: function (newPage) {
                                    getData(newPage);
                                    $('#r').html('Page # ' + newPage);
                                }
                            });
                        }

                        defineButtons();
                    }),
                    error: (function Error(XMLHttpRequest, status, error) {
                        var err = eval("(" + XMLHttpRequest.responseText + ")");
                        $("#location").html(err.Message).fadeIn(1000);
                    })
                });
            }

            function defineButtons() {
                $("#gridLocation a.btnNew").button();

                $("#gridLocation a.btnDelete").click(function () {

                    var result = confirm("Are you sure you want to delete this record?");
                    if (result) {
                        var aTag = $(this);

                        // load provider
                        $.ajax({
                            type: "POST",
                            url: "/Update.asmx/DeleteLocation",
                            data: { locationID: aTag.attr("locationid") },
                            contentType: "application/json; charset=utf-8",
                            dataType: "json",
                            async: false,
                            success: (function Success(data, status) {
                                if (data.d) {
                                    // Select provider
                                    $('#gridLocation > tbody > tr').removeClass('selected');
                                    aTag.parents("tr:first").remove();
                                    return false;
                                }
                                else {

                                }
                            }),
                            error: (function Error(request, status, error) {
                                $("#location").html(request.statusText).fadeIn(1000);
                            })
                        });
                    }
                });
            }

            function loading(tag) {
                tag.empty().html('<img src="/Images/ajax-loader.gif" />');
            }

            getData(1, true);
        });
        
    </script>

</asp:Content>
